<template>
  <view>
    <!--    补卡-->
    <HeaderNavTabarSvg :title="title">
      <image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
    </HeaderNavTabarSvg>
    <view class="textarea-box">
      <text class="info-list-left isrequired">加班事由</text>
      <textarea v-model="bukaData.bakahsiyou" class="textinfo-list" placeholder="请输入"></textarea>
    </view>
    <view class="picker-content" @tap="toggle">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">开始时间</text>
        <view class="info-list-right">
          <view class="uni-input" v-if="bukaData.datetimeValue">{{ bukaData.datetimeValue }}</view>
          <view class="uni-input" v-else>请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="datetimePicker"
      v-model="time"
      :formatter="formatter"
      mode="datetime"
      @confirm="confirm"
    >
    </uv-datetime-picker>
    <view class="picker-content" @tap="toggle2">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">结束时间</text>
        <view class="info-list-right">
          <view class="uni-input" v-if="bukaData.datetimeValue2">{{
            bukaData.datetimeValue2
          }}</view>
          <view class="uni-input" v-else>请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="datetimePicker2"
      :minDate="bukaData.datetimeValue"
      v-model="time"
      :formatter="formatter"
      mode="datetime"
      @confirm="confirm2"
    >
    </uv-datetime-picker>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">加班时长</text>
        <view class="info-list-right">
          <view class="uni-input" v-if="bukaData.datetimeValue2 && bukaData.datetimeValue">
            {{ dayjs(bukaData.datetimeValue2).diff(dayjs(bukaData.datetimeValue), 'hour') }}
            <text>小时</text>
          </view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <!--    审批流程-->
    <AuditApplicationSteps
      :ruleId="'jiaban'"
      :time="dayjs(bukaData.datetimeValue2).diff(dayjs(bukaData.datetimeValue), 'hour')"
    />
    <!--    底部占位符-->
    <view class="blank"></view>
    <!--    保存-->
    <view class="buttonContainer">
      <button class="buttonClass" @click="clickSave">提交</button>
    </view>
  </view>
</template>

<script setup>
import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
import { onLoad } from '@dcloudio/uni-app'

import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
import { ref } from 'vue'
import dayjs from 'dayjs'
import { attendanceApplyApi } from '@/api/attendance'
import { getUserProfile } from '@/api/system/user'
import approvalProcessState from '@/store/modules/approvalProcess'
import { isObjectValuesNotEmpty } from '@/utils/objIsnull.js'
const title = '加班'
const deptId = ref(null)
const userId = ref(null)
const popup = ref(null)
const datetimePicker = ref(null)
const datetimePicker2 = ref(null)
let time = dayjs()
const listTime = ref([
  {
    title: '上班-正常(07:56)',
    txt: '应08:00上班',
    time: '07:56',
  },
  {
    title: '下班-正常(12:04)',
    txt: '应08:00上班',
    time: '07:56',
  },
  {
    title: '上班-正常(13:56)',
    txt: '应08:00上班',
    time: '13:56',
  },
  {
    title: '上班-正常(18:05)',
    txt: '应08:00上班',
    time: '18:05',
  },
])

const goHistoryView = () => {
  uni.navigateTo({
    url: '/components/TypeDefineModel?type=加班',
  })
}
let bukaData = ref({
  datetimeValue: '',
  datetimeValue2: '',
  bakahsiyou: '',
})
// 打开选择器
const toggle = (type) => {
  datetimePicker.value.open()
}

const toggle2 = (type) => {
  if (bukaData.value.datetimeValue) {
    datetimePicker2.value.open()
  } else {
    uni.showToast({
      title: '请先选择开始时间',
      icon: 'none',
    })
  }
}

const confirm = (obj) => {
  bukaData.value.datetimeValue = dayjs(obj.value).format('YYYY-MM-DD HH:mm')
}

const confirm2 = (obj) => {
  bukaData.value.datetimeValue2 = dayjs(obj.value).format('YYYY-MM-DD HH:mm')
}

const formatter = (type, value) => {
  if (type === 'year') {
    return `${value}年`
  }
  if (type === 'month') {
    return `${value}月`
  }
  if (type === 'day') {
    return `${value}日`
  }
  if (type === 'hour') {
    return `${value}时`
  }
  if (type === 'minute') {
    return `${value}分`
  }
  return value
}
const clickSave = async () => {
  if (isObjectValuesNotEmpty(bukaData.value)) {
    let params = {
      deptId: deptId.value,
      userId: userId.value,
      reason: bukaData.value.bakahsiyou,
      startTime: new dayjs(bukaData.value.datetimeValue).format('YYYY-MM-DD HH:mm:ss'),
      endTime: new dayjs(bukaData.value.datetimeValue2).format('YYYY-MM-DD HH:mm:ss'),
      duration: dayjs(bukaData.value.datetimeValue2).diff(
        dayjs(bukaData.value.datetimeValue),
        'hour',
      ),
      type: '加班',
      ruleId: 'jiaban',
      approve: approvalProcessState.state.approvalProcess,
    }
    let { code } = await attendanceApplyApi(params)
    if (code === 200) {
      uni.showToast({
        title: '提交成功',
        duration: 1500,
        icon: 'success',
      })
      setTimeout(() => {
        uni.navigateBack({
          url: '/pages/attendance/attendancePage',
        })
      }, 1000)
    }
  } else {
    uni.showToast({
      title: '请输入完整数据',
      icon: 'error',
    })
  }
}
const getUserProfileFn = async () => {
  let { code, data } = await getUserProfile()
  if (code === 200) {
    deptId.value = data.deptId
    userId.value = data.userId
  }
}
onLoad(async () => {
  await getUserProfileFn()
})
</script>

<style lang="scss" scoped>
@import '@/static/style/index.scss';

.bukaSvg {
  width: 32rpx;
  height: 32rpx;
}

.buttonContainer {
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: center;
  width: 100%;
  align-items: center;
  padding: 16rpx;
  background-color: #fff;
}

.buttonClass {
  display: flex;
  width: 686rpx;
  height: 72rpx;
  justify-content: center;
  align-items: center;
  background: #165dff;
  border-radius: 16rpx;
  color: #fff;
  font-size: 32rpx;
  //margin-top: 40rpx;
  overflow: hidden;
}

.blank {
  height: calc(72rpx + 32rpx + 20rpx);
}

.bukabanci {
  .info-content {
    color: #86909c !important;
  }

  .info-list-left {
    // @include font-color-auxiliary;
    color: #86909c !important;
  }
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.popup-content {
  background: #f7f8fa;

  .header-box {
    padding: 30rpx 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    p {
      color: #1d2129;
      font-size: 34rpx;
      font-weight: 500;
    }

    .module-image-ba {
      border-radius: 32rpx;
      background: #f2f3f5;
      width: 45rpx;
      height: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }

  .card-list {
    display: flex;
    align-items: center;
    flex-direction: column;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .list {
      box-shadow: 0 4rpx 16rpx 0 #0000000d;
      box-sizing: border-box;
      width: 100%;
      background: #fff;
      padding: 16rpx 24rpx;
      border-radius: 8rpx;
      margin-bottom: 10rpx;

      h4 {
        color: #1d2129;
        font-size: 32rpx;
        font-weight: 400;
      }

      p {
        color: #86909c;
        font-size: 24rpx;
        font-weight: 400;
        margin-top: 10rpx;
      }
    }
  }
}

.measurement-info-list {
  @include flex($align: center, $justify: space-between);
  padding: $spacing-row-base;
  background: #fff;
  margin-top: 16rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .info-list-right {
    @include flex($align: center);

    .info-content {
      @include textStyleBase;
      font-size: 34rpx;
    }

    .iconfont {
      margin-left: 16rpx;
      // @include font-color-auxiliary;
      color: $uni-fc-fu-lin;
    }
  }

  .unit {
    // @include font-color-auxiliary;
    color: $uni-fc-fu-lin;
  }
}
</style>
